package cn.xsly.ymcc.config;

import cn.xsly.ymcc.web.controller.QuartZController;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SchedulerConfig {

    /**
    * 定义任务
    */
	@Bean
	public JobDetail testJob() {
		JobDataMap data = new JobDataMap(); //通过JobDataMap向任务中传递参数
		return JobBuilder.newJob(QuartZController.class).withIdentity("TestJob").usingJobData(data).storeDurably()
				.build();
	}

    /**
    * 定义触发器
    */
	@Bean
	public Trigger syncPwdTrigger(@Qualifier("testJob") JobDetail job) {
		ScheduleBuilder<?> csb = getScheduleBuilderWithMillisecond(5000); // 每5秒运行一次

		return TriggerBuilder.newTrigger().forJob(job).withIdentity("TestTrigger").withSchedule(csb).build();
	}

	private ScheduleBuilder<?> getScheduleBuilderWithMillisecond(long milliSecond) {
		SimpleScheduleBuilder ssb = SimpleScheduleBuilder.simpleSchedule().withIntervalInMilliseconds(milliSecond).repeatForever();
		return ssb.repeatForever();
	}
}
